package com.yanqu.road.server.cmd.cross.marriage.world;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.entity.marriage.MarriageRecord;
import com.yanqu.road.entity.marriage.MarriageResult;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.pb.marriage.MarriageProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_CROSS_WORLD_MARRIAGE_AGREE_RESP, desc = "跨服联姻同意响应（跨服商会分组）")
public class CrossWorldMarriageAgreeRespCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg reqMsg = CrossMarriageProto.CrossMarriageAgreeCrossDealRespMsg.parseFrom(yanQuMessage.getBytes());
        int ret = reqMsg.getRet();
        MarriageInfo marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
        MarriageInfo partnerMarriage = null;
        if(0 == ret){
            partnerMarriage = MarriagePb.parseMarriageInfo(reqMsg.getPartnerData());
        }
        //处理结果
        MarriageResult marriageResult = MarriageMgr.agreeCrossWorldMarriageResponse(ret, marriageInfo, partnerMarriage);
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(yanQuMessage.getPlayerId());
        if(null != player){
            MarriageProto.MarriageApplyDealRespMsg.Builder respMsg = MarriageProto.MarriageApplyDealRespMsg.newBuilder();
            respMsg.setRet(marriageResult.getRet());
            if(0 == marriageResult.getRet()) {
                MarriageRecord marriageRecord = marriageResult.getMarriageRecord();
                if (null != marriageRecord) {
                    String reward = PropertyHelper.parsePropertyToString(marriageRecord.getPartnerReward());
                    respMsg.setReward(reward);
                    MarriageProto.MarriageRecordTempMsg.Builder msg = MarriagePb.parseMarriageRecordTempMsg(marriageRecord);
                    if (null != msg) {
                        respMsg.setMarriageData(msg);
                    }
                }
            }
            player.sendPacket(Protocol.U_MARRIAGE_APPLY_DEAL, respMsg);
        }

    }
}
